package com.lkd.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.lkd.dto.LoginDto;
import com.lkd.entity.User;
import com.lkd.result.Result;
import com.lkd.service.UserService;
import com.lkd.utils.JwtUtil;
import com.lkd.vo.LoginUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/api/user-service/user")
@Slf4j
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody LoginDto dto){
        log.info("用户登录：{}",dto);
        LoginUserVo user= userService.login(dto);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id",user.getUserId());
        hashMap.put("username",user.getUserName());
        long singer = System.currentTimeMillis() + 12 * 60 * 60 * 1000;
        String token = JwtUtil.createJWT("lkd", singer, hashMap);

        user.setToken(token);
        return Result.success(user);
    }

}
